package com.example.web;


import com.example.fanqie.entity.Author;
import com.example.fanqie.entity.Bookshelf;
import com.example.fanqie.entity.Category;
import com.example.fanqie.entity.Novel;
import com.example.fanqie.mapper.BookshelfMapper;
import com.example.fanqie.mapper.NovelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class BookshelfDao {

    @Resource
    private BookshelfMapper bookshelfMapper;

    @Resource
    private NovelMapper novelMapper;




    @RequestMapping("selectbookshelf")
    public List<Bookshelf> selectbookshelf( @RequestParam("userId") Integer userId){
        List<Bookshelf> bookshelves = bookshelfMapper.selectuserId(userId);

        for (Bookshelf book : bookshelves) {

            Novel novel = novelMapper.selectnovelId(book.getNovelId());
            book.setTitle(novel.getTitle());
            book.setCoverImage(novel.getCoverImage());
        }
        return  bookshelves;

    }


     @RequestMapping("deletebookshelf")
    public int deletebookshelf( @RequestParam("bookshelfId") Integer bookshelfId){
        Bookshelf bookshelf =new Bookshelf();
        bookshelf.setBookshelfId(bookshelfId);
        bookshelf.setDlt(0);

         int i = bookshelfMapper.updateById(bookshelf);
         return i;
     }


    @RequestMapping("addBookshelf")
    public int addBookshelf(@RequestBody Bookshelf bookshelf){
        //dlt 不一定为 1
        Bookshelf bookshelf1 = bookshelfMapper.selectbookshelf2(bookshelf.getNovelId(),bookshelf.getUserId());

        if(bookshelf1!=null){
            if(bookshelf1.getDlt() == 0){
                bookshelf1.setDlt(1);
            }
            int i = bookshelfMapper.updateById(bookshelf1);
            return i;

        }
        else{
            int insert = bookshelfMapper.insert(bookshelf);
            return insert;

        }

    }

    @RequestMapping("isInBookshelf")
    public Bookshelf isInBookshelf(@RequestBody Bookshelf    bookshelf){
        //dlt 一定为 1
        Bookshelf bookshelf1 = bookshelfMapper.selectbookshelf1(bookshelf.getNovelId(),bookshelf.getUserId());

        return bookshelf1;

    }

    @RequestMapping("updatabookshelf")
    public int updatabookshelf(@RequestBody Bookshelf bookshelf){

        int i = bookshelfMapper.updateById(bookshelf);
        return i;

    }







}
